package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;

import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
//import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-03
 */
@RestController
@RequestMapping("/eduservice/edu-teacher")
@Api(description = "讲师管理")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("item", list);
    }

    @ApiOperation("删除指定讲师")
    @DeleteMapping("delete/{id}")
    public R delete(@ApiParam(name = "id", value = "讲师ID", required = true)
                    @PathVariable String id) {
        boolean f = eduTeacherService.removeById(id);
        if (f)
            return R.ok();
        return R.error();
    }

    @ApiOperation("分页查询讲师")
    @GetMapping("pageList/{page}/{limit}")
    public R pageList(@ApiParam(name = "page", value = "页数", required = true) @PathVariable int page,
                      @ApiParam(name = "limit", value = "每页长度", required = true) @PathVariable int limit) {

        Page<EduTeacher> eduTeacherPage = new Page<>(page, limit);
        eduTeacherService.page(eduTeacherPage, null);
        List<EduTeacher> records = eduTeacherPage.getRecords();
        long total = eduTeacherPage.getTotal();
        return R.ok().data("total", total).data("rows", records);

    }

    @ApiOperation("带条件的分页查询讲师")
    @PostMapping("pageListCondition/{current}/{limit}")
    public R pageListCondition(@ApiParam(name = "current", value = "页数", required = true) @PathVariable("current") int current,
                               @ApiParam(name = "limit", value = "每页长度", required = true) @PathVariable int limit,
                               @RequestBody(required=false) TeacherQuery teacherQuery) {
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<EduTeacher>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }

        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(eduTeacherPage, wrapper);
        List<EduTeacher> records = eduTeacherPage.getRecords();
        long total = eduTeacherPage.getTotal();
        return R.ok().data("total", total).data("rows", records);

    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R save(@RequestBody EduTeacher eduTeacher) {
        boolean f = eduTeacherService.save(eduTeacher);
        if (f)
            return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("item", eduTeacher);
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("updateById")
    public R updateById(@RequestBody EduTeacher eduTeacher) {
        boolean f = eduTeacherService.updateById(eduTeacher);
        if (f) return R.ok();
        return R.error();
    }

//    @GetMapping("update")
//    public String update(){
//        EduTeacher eduTeacher = new EduTeacher();
//        eduTeacher.setId("1");
//        eduTeacher.setLevel(5);
//        boolean f = eduTeacherService.updateById(eduTeacher);
//        return "1";
//    }

}

